﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YXQ.Dao.ViewModel;
using AutoMapper;
namespace YXQ.Dao
{

    public class MudleDao
    {


        public MudleDao()
        {

            Mapper.CreateMap<Y_Mudle, MudleModel>();
        }
        public List<Y_Mudle> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                recordcount = edm.Y_Mudle.Count();

                return edm.Y_Mudle.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public MudleModel GetOneById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_Mudle.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<Y_Mudle, MudleModel>(query);
            }

        }


        public List<MudleModel> GetListByPid(int pid)
        {
            using (YXQManageEntities edm = new YXQManageEntities()) {
                return Mapper.Map<List<Y_Mudle>, List<MudleModel>>(edm.Y_Mudle.Where(s=>s.ParentID==pid).ToList());
            }
        }



        public int Save(MudleModel obj)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_Mudle.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new Y_Mudle();

                    edm.AddToY_Mudle(query);

                }


                query.Title = obj.Title;

                query.Remark = obj.Remark;

                query.PageUrl = obj.PageUrl;

                query.ParentID = obj.ParentID;

                query.FunCode = obj.FunCode;

                query.OrderId = obj.OrderId;

                edm.SaveChanges();

                return query.Id;


            }

        }


        public int DelById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_Mudle.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

